package io.github.dunwu.javacore.jdk8.concurrent;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.StampedLock;

/**
 * @author Benjamin Winterberg
 */
public class Lock5 {

	public static void main(String[] args) {
		ExecutorService executor = Executors.newFixedThreadPool(2);

		StampedLock lock = new StampedLock();

		executor.submit(() -> {
			long stamp = lock.tryOptimisticRead();
			try {
				System.out.println("Optimistic Lock Valid: " + lock.validate(stamp));
				ConcurrentUtils.sleep(1);
				System.out.println("Optimistic Lock Valid: " + lock.validate(stamp));
				ConcurrentUtils.sleep(2);
				System.out.println("Optimistic Lock Valid: " + lock.validate(stamp));
			} finally {
				lock.unlock(stamp);
			}
		});

		executor.submit(() -> {
			long stamp = lock.writeLock();
			try {
				System.out.println("Write Lock acquired");
				ConcurrentUtils.sleep(2);
			} finally {
				lock.unlock(stamp);
				System.out.println("Write done");
			}
		});

		ConcurrentUtils.stop(executor);
	}

}
